package com.anche.demo.util;

import okhttp3.*;

import java.io.File;
import java.io.IOException;

/**
 * @Classname HttpUtils
 * @Description TODO
 * @Date 2024/1/21 19:48
 * @Created by Administrator
 */
public class HttpUtils {

  public static   void postByFrom(){
        OkHttpClient client = new OkHttpClient();

        // 构建form-data请求体
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("field1", "value1")
                .addFormDataPart("field2", "value2")
                .addFormDataPart("file", "filename.txt",
                        RequestBody.create(MediaType.parse("application/octet-stream"), new File("C:\\Users\\Administrator.DESKTOP-P4JV3M0\\Desktop\\导入.xlsx")))
                .build();

        // 构建请求
        Request request = new Request.Builder()
                .url("http://127.0.0.1:8080/upload")
                .post(requestBody)
                .build();

        // 发送请求并获取响应
        try (Response response = client.newCall(request).execute()) {
            String responseBody = response.body().string();
            // 处理响应
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}